package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import admin.bean.Subject;

import leader.bean.Leader;

import database.dao.MysqlCrud;
import teacher.bean.Award;
import teacher.bean.Expertclass;
import teacher.bean.Paper;
import teacher.bean.Patent;
import teacher.bean.Project;
import teacher.bean.Teacher;
import teacher.bean.Treatise;
import tools.StringDate;

public class LeaderService {
	
	/**
	 * @category 查询方向信息
	 * @param lea
	 * @return
	 */
	public Leader selectLeader(Teacher lea) {
		
		String sql="select * from leader as L,teacher as T where L.leaId = T.direction and T.direction=?";
		String[] parameter={String.valueOf(lea.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql,parameter);
		if(list.size()>0) {
			Map map=list.get(0);
			Leader leader = new Leader();
			leader.setLeaid(Integer.parseInt(map.get(0).toString()));
			leader.setName(map.get(1).toString());
			leader.setSubjectid(Integer.parseInt(map.get(2).toString()));
			leader.setTeacherid(Integer.parseInt(map.get(3).toString()));
			leader.setIntroduction(map.get(4).toString());
			leader.setRemarks(map.get(5).toString());
			leader.setMailbox(map.get(6).toString());
			return leader;			
		} else {
			return null;
		}		
	}
	/**
	 * @category 更新方向信息
	 * @param lea
	 * @param leader
	 * @return
	 */
	public boolean updateLeader(Teacher lea,Leader leader) {
		boolean flag = false;
		String sql="update leader set email = ?, introduction = ?, remarks = ? where leaId = ?";
		String[] parameter={
				String.valueOf(leader.getMailbox()),
				String.valueOf(leader.getIntroduction()),
				String.valueOf(leader.getRemarks()),
				String.valueOf(lea.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	/**
	 * @category 查询方向所属的学科
	 * @param lea
	 * @return
	 */
	public Subject selectSubject(Teacher lea) {
		Subject sub = new Subject();
		String sql="select * from subject as S ,Teacher as T where S.subId = T.subject and T.direction=?";
	//	String sql="select * from subject where subId = ?";
		String[] parameter={String.valueOf(lea.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql,parameter);
		if(list.size()>0) {
			Map map=list.get(0);
			sub.setSubId(Integer.parseInt(map.get(0).toString()));
			sub.setName(map.get(1).toString());
			sub.setUniversity(map.get(2).toString());
			sub.setArea(map.get(3).toString());
			sub.setChargePerson(map.get(4).toString());
			sub.setMainWeb(map.get(5).toString());
			sub.setEmail(map.get(6).toString());
			sub.setSuperLevelDate(map.get(7).toString());
			sub.setMainDirection(map.get(8).toString());
			sub.setRemark(map.get(9).toString());
			return sub;
		}
		return null;
	}
	/**
	 * @category 查询方向负责人的信息
	 * @param lea
	 * @return
	 */
	public Teacher selectTeacher(Teacher lea) {
		System.out.println(lea.getName());
		System.out.println(lea.getDirection());
		Teacher teacher = new Teacher();
		String sql="select * from Teacher where direction=? and power =?";
		String[] parameter={
				String.valueOf(lea.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql,parameter);
		if(list.size() > 0) {
			Map map=list.get(0);
			try {
				teacher.setTeaId(Integer.parseInt(map.get(0).toString()));
				teacher.setName((String) map.get(1));
				teacher.setUser((String) map.get(2));
				teacher.setPassword((String) map.get(3));
				teacher.setSubject(Integer.parseInt(map.get(4).toString()));
				teacher.setDirection(Integer.parseInt(map.get(5).toString()));
				teacher.setDuty(map.get(6).toString());
				teacher.setDegree(Integer.parseInt(map.get(7).toString()));
				teacher.setOnline(Integer.parseInt(map.get(8).toString()));
				teacher.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(9).toString()));
				teacher.setFurStud(map.get(10).toString());
				teacher.setTopSchool(map.get(11).toString());
				teacher.setTopMajor(map.get(12).toString());
				teacher.setMail(map.get(13).toString());
				teacher.setPower(Integer.parseInt(map.get(14).toString()));
				teacher.setInSubDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(15).toString()));
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}		
			return teacher;
		} else {
			return null;
		}		
	}
	
	public boolean insertExpertclass(Expertclass experter) {
		boolean flag = false;
		String sql = "insert into expertclass(expert)"+" value(?)";
		String[] parameter = {
				String.valueOf(experter.getExpert()),
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	public List<Expertclass> selectAllExpertclass() {
		List<Expertclass> list = new ArrayList<Expertclass>();
		String sql="select * from expertclass";
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, null);
		for(int i=0;i<lists.size();i++) {
			Map map=lists.get(i);
			try {
				Expertclass experter = new Expertclass();
				experter.setExpId(Integer.parseInt(map.get(0).toString()));
				experter.setExpert(map.get(1).toString());
				list.add(experter);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public int SelectTeaIdByUser(int user){
		int TeaId;
		String sql="select teaId from teacher where user=?";
		String[] parameter = {
				String.valueOf(user),
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, parameter);
		Map map = lists.get(0);
		TeaId = Integer.parseInt(map.get(0).toString());
		return TeaId;
	}
	public boolean insertMidteaexp(int teacherid,int expertid,Date date) {
		boolean flag = false;
		String sql = "insert into Midteaexp(teaId,expId,date)"+" value(?,?,?)";
		String[] parameter = {
				String.valueOf(teacherid),
				String.valueOf(expertid),
				String.valueOf(date)
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	/**
	 * @category 插入一个方向的教师
	 * @author chenkai
	 * @param teacher Teacher 插入的教师信息
	 * @return flag boolean 插入成功返回true，否则返回false
	 */
	public boolean insertTeacher(Teacher teacher) {
		boolean flag = false;
		String sql = "insert into teacher(name,user,password,subject,direction,duty,degree,online,birthday,furStud,topSchool,topMajor,mail,power,insubdate)"+" value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		String[] parameter = {
				String.valueOf(teacher.getName()),
				String.valueOf(teacher.getUser()),
				String.valueOf(teacher.getPassword()),
				String.valueOf(teacher.getSubject()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(teacher.getDuty()),
				String.valueOf(teacher.getDegree()),
				String.valueOf(teacher.getOnline()),
				String.valueOf(teacher.getBirthday()),
				String.valueOf(teacher.getFurStud()),
				String.valueOf(teacher.getTopSchool()),
				String.valueOf(teacher.getTopMajor()),
				String.valueOf(teacher.getMail()),
				String.valueOf(teacher.getPower()),
				String.valueOf(teacher.getInSubDate())
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 查询一个方向的所有教师
	 * @author chenkai
	 * @param teacher Teacher 方向信息
	 * @return teach List<Teacher> 返回一个List<Teacher>的教师集合
	 */
	@SuppressWarnings("deprecation")
	public List<Teacher> selectAllDirectTeacher(Teacher teacher) {
		List<Teacher> teach = new ArrayList<Teacher>();
		String sql="select * from teacher where direction=?";
		String[] parameter={String.valueOf(teacher.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			try {
				Teacher tea = new Teacher();
				tea.setTeaId(Integer.parseInt(map.get(0).toString()));
				tea.setName((String) map.get(1));
				tea.setUser((String) map.get(2));
				tea.setPassword((String) map.get(3));
				tea.setSubject(Integer.parseInt(map.get(4).toString()));
				tea.setDirection(Integer.parseInt(map.get(5).toString()));
				tea.setDuty(map.get(6).toString());
				tea.setDegree(Integer.parseInt(map.get(7).toString()));
				tea.setOnline(Integer.parseInt(map.get(8).toString()));
				tea.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(9).toString()));
				tea.setFurStud(map.get(10).toString());
				tea.setTopSchool(map.get(11).toString());
				tea.setTopMajor(map.get(12).toString());
				tea.setMail(map.get(13).toString());
				tea.setPower(Integer.parseInt(map.get(14).toString()));
				tea.setInSubDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(15).toString()));
				teach.add(tea);			
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return teach;
	}
	

	
//	/**
//	 * @category 查询同一研究方向的所有毕业论文
//	 * @author chenkai
//	 * @param teacher Teacher 研究方向信息
//	 * @return lists List<Degreepaper> 返回以List<Degreepaper>的毕业论文集合
//	 */
//	public List<Degreepaper> selectAllDirectDegreepaper(Teacher teacher) {
//		List<Degreepaper> lists = new ArrayList<Degreepaper>();
//		String sql="select * from Degreepaper as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=?";
//		String[] parameter={String.valueOf(teacher.getDirection())};
//		MysqlCrud mysqlcurd=new MysqlCrud();
//		List<Map> list= mysqlcurd.SelectData(sql, parameter);
//		for(int i=0;i<list.size();i++) {
//			Map map=list.get(i);
//			try {
//				Degreepaper degreepaper = new Degreepaper();
//				degreepaper.setDegId(Integer.parseInt(map.get(0).toString()));
//				degreepaper.setName(map.get(1).toString());
//				degreepaper.setStuName(map.get(2).toString());
//				StringDate date = new StringDate();
//				degreepaper.setLeaveDate(date.StringToDate(map.get(3).toString()));
//				degreepaper.setOther(map.get(4).toString());
//				degreepaper.setTeacher(map.get(5).toString());
//				degreepaper.setTeaId(Integer.parseInt(map.get(6).toString()));
//				lists.add(degreepaper);			
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//			}
//		}
//		return lists;
//	}
	
	
	

	

	

	
	/**
	 * @category 得到一个方向的所有教师的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectTeacher(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from teacher where direction = ?";
		String[] parameter={String.valueOf(teacher.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
		
	/**
	 * @category 得到一个方向的所有毕业论文的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectDegreepaper(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from Degreepaper as acheve ,teacher as T where acheve.teaId = T.teaId and T.user=?";
		String[] parameter={String.valueOf(teacher.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
